using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using MT.eVotePRS.Common;
using MT.eVotePRS.Device;
using MT.eVotePRS.Model;

namespace MT.eVotePRS.UI
{
    public partial class frmRptIndividual : Form
    {
        public frmRptIndividual()
        {
            InitializeComponent();

            txtHandsetID.KeyDown += new KeyEventHandler(txtHandsetID_KeyDown);
            txtStudentID.KeyDown += new KeyEventHandler(txtStudentID_KeyDown);
        }

        void frmRptIndividual_Load(object sender, EventArgs e)
        {
            rbtHandsetID.Checked = true;
            
            Program.ArsAPIHelper.StartProcess();
        }

        void frmRptIndividual_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.ArsAPIHelper.StopProcess();
        }

        void rbtHandsetID_CheckedChanged(object sender, EventArgs e)
        {
            txtHandsetID.Enabled = rbtHandsetID.Checked;
            txtStudentID.Enabled = !rbtHandsetID.Checked;
        }

        void rbtStudentID_CheckedChanged(object sender, EventArgs e)
        {
            txtHandsetID.Enabled = rbtHandsetID.Checked;
            txtStudentID.Enabled = !rbtHandsetID.Checked;
        }

        void txtStudentID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnGen_Click(this, EventArgs.Empty);
            }
        }

        void txtHandsetID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnGen_Click(this, EventArgs.Empty);
            }
        }

        delegate void _dataReceived(object sender, ARSEventArgs e);
        void dataReceiver_DataReceived(object sender, ARSEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new _dataReceived(dataReceiver_DataReceived), sender, e);
                return;
            }

            HandsetData keypadData = (HandsetData)e.ReceivedData;
            switch (keypadData.CommandCode)
            {
                case ARSCommandCode.STUDENT_JOIN:
                    if (rbtHandsetID.Checked)
                    {
                        txtHandsetID.Text = keypadData.MacStr;
                    }
                    else if (rbtStudentID.Checked)
                    {
                        txtStudentID.Text = keypadData.UserID;
                    }

                    break;

                default:
                    break;
            }
        }


        void btnGen_Click(object sender, EventArgs e)
        {
            if (rbtHandsetID.Checked && txtHandsetID.Text.Trim().Length <= 0)
            {
                DialogBox.boxWarning("Invalid Handset ID!", "Warning!");
                txtHandsetID.SelectAll();
                return;
            }
            if (rbtStudentID.Checked && txtStudentID.Text.Trim().Length <= 0)
            {
                DialogBox.boxWarning("Invalid Student ID!", "Warning!");
                txtStudentID.SelectAll();
                return;
            }

            try
            {
                if (rbtHandsetID.Checked && txtHandsetID.Text.Trim().Length > 0)
                {                     
                    string handsetId = txtHandsetID.Text.Trim();

                    new MT.eVotePRS.Presentation.Logic.ReportActions().ShowHandsetEvents(handsetId);
                }
                else if (rbtStudentID.Checked && txtStudentID.Text.Trim().Length > 0)
                {
                    string studentId = txtStudentID.Text.Trim();

                    new MT.eVotePRS.Presentation.Logic.ReportActions().ShowStudentEvents(studentId);
                }
            }
            catch (Exception ex)
            {
                DialogBox.boxWarning("Exception: " + ex.Source + "; " + "Message: " + ex.Message);
                return;
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }

            DialogBox.boxWarning("No history in Inventory!", "Warning!");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}